package fr.thomascoffin.mocaf;

import org.jetbrains.annotations.NotNull;

import java.util.Comparator;

/**
 * Provides a wrapper for an existing comparator that extends NullableComparator, so that the resulting comparator
 * will accept null values.
 *
 * @see NullableComparator
 *      <p/>
 *      <p/>
 *      This class is released by Thomas Coffin (thomas.coffin@gmail.com) under the <a href="http://www.gnu.org/copyleft/lesser.html" target="_blank">LGPL License</a>
 *      as a component of the <a href="http://code.google.com/p/mocaf" target="_blank">mocaf project</a>
 *      <p/>
 *      (c) Thomas Coffin 2008.
 */
public class NullableComparatorWrapper<T> extends NullableComparator<T>
{
	private final Comparator<T> _comparator;

	/**
	 * Wraps the given comparator in a null-value-aware comparator.
	 *
	 * @param comparator							 the comparator to wrap (which may not be null-compliant).
	 * @param nullIsLessThanEverything true if null values must be considered to be less than any not null value, false
	 *                                 if they must be considered greater than any not null value.
	 */
	public NullableComparatorWrapper(Comparator<T> comparator, boolean nullIsLessThanEverything)
	{
		super(nullIsLessThanEverything);
		_comparator = comparator;
	}

	/**
	 * Wraps the given comparator in a null-value-aware comparator, that will implement the default behaviour,
	 * which considers than any null value is less than any not null value.
	 *
	 * @param comparator the comparator to wrap (which may not be null-compliant).
	 */
	public NullableComparatorWrapper(Comparator<T> comparator)
	{
		super();
		_comparator = comparator;
	}

	protected int compareNotNull(@NotNull T o1, @NotNull T o2)
	{
		return _comparator.compare(o1, o2);
	}
}
